package com.wwg.market.ui.category.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.*;
import com.mobileares.midp.widgets.client.anchor.TextAnchor;
import com.mobileares.midp.widgets.client.list.ListItem;
import com.mobileares.midp.widgets.client.list.ListWidget;
import com.mobileares.midp.widgets.client.utils.IWidgetProvider;
import com.wwg.market.ui.service.dto.GeneralCodeDto;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-12-27
 * Time: 15:20:25
 * To change this template use File | Settings | File Templates.
 */
public class PropertyItemWidget extends Composite {
    private VerticalPanel container;
    private ListWidget list;

    public PropertyItemWidget() {
        container = new VerticalPanel();
        initWidget(container);
        init();
    }

    public List<GeneralCodeDto> getCodes() {
        List result = new ArrayList();
        for (Iterator it = list.getIterator(); it.hasNext();) {
            result.add(((ListItem) it.next()).getUserObject());
        }
        return result;
    }

    public void setValue(List<GeneralCodeDto> dtos) {
        if (dtos != null && dtos.size() > 0) {
            list.setVisible(true);
            list.removeAll();
            for (GeneralCodeDto dto : dtos) {
                list.addItem(dto);
            }
        } else {
            list.setVisible(false);
            list.removeAll();
        }

    }

    private void init() {
        TextAnchor add = new TextAnchor("添加代码");
        add.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                list.setVisible(true);
                list.insertItem(0, new ListItem(new GeneralCodeDto()), true);
            }
        });
        initList();
        container.add(add);
        container.setCellHorizontalAlignment(add, HorizontalPanel.ALIGN_RIGHT);
        container.add(list);
    }

    private void initList() {
        list = new ListWidget();
        list.setProvider(new IWidgetProvider() {
            public Widget getWidget(Object ob) {
                GeneralCodeDto dto = (GeneralCodeDto) ob;
                return getListCell(dto);
            }

            public Object getUserObject(Object ob) {
                return ob;
            }
        });
    }

    private HorizontalPanel getListCell(final GeneralCodeDto dto) {
        HorizontalPanel hp = new HorizontalPanel();
        hp.setWidth("180px");
        final TextBox name = new TextBox();
        name.setStyleName("property-code-input");
        final TextBox code = new TextBox();
        code.setStyleName("property-code-input");
        final TextAnchor delete = new TextAnchor("删");
        hp.add(name);
        hp.add(code);
        hp.add(delete);
        hp.setCellWidth(name, "60px");
        hp.setCellWidth(code, "60px");
        hp.setCellWidth(delete, "20px");
        name.setValue(dto.getName());
        code.setValue(dto.getCode());
        ValueChangeHandler handler = new ValueChangeHandler<String>() {
            public void onValueChange(ValueChangeEvent<String> stringValueChangeEvent) {
                dto.setName(name.getValue());
                dto.setCode(code.getValue());
            }
        };
        name.addValueChangeHandler(handler);
        code.addValueChangeHandler(handler);
        delete.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                list.getItem(dto).removeFromParent();
            }
        });
        return hp;
    }
}
